package com.airS.task.fileMgr.security.shiro.filter;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.airS.task.fileMgr.common.response.BaseResponse;
import com.airS.task.fileMgr.common.response.ResponseCode;
import com.airS.task.fileMgr.utils.servlet.ServletUtils;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Service;

/**
 * 用户验证过滤器(只需认证)
 * Created by alan on 2017/3/14.
 */
public class UserFilter extends org.apache.shiro.web.filter.authc.UserFilter {

	@Override
	protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		if (!"XMLHttpRequest".equalsIgnoreCase(httpServletRequest.getHeader("X-Requested-With"))) {
			WebUtils.issueRedirect(request, response, getLoginUrl(), null, true);
		} else {
			BaseResponse baseResponse = new BaseResponse();
			baseResponse.setCode(ResponseCode.LOGIN_OUT);
			ServletUtils.writeJson(baseResponse, response);
		}
	}

}
